/******************************************************************************************************
Copyright (C) 2008 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

// This is an example for saving custom, mandatory fields to MnDB. 
// To use: 
// 1. Create a new database with two added custom fields "Project" and "NotebookRef" in the Molecule item type.
// 2. Place this script in one of the Mnova scripts folders.
// 3. Start saving to the new db. A dialog asking for the content of the mandatory fields will show with each save operation.        
// 4. Customize the function dbPreSaveHook according to your needs.

// This function, when in the global namespace, is called when saving to database, after selecting the items 
// to be saved but before sending the data to the server.
// Parameters: aDBClient - DBClient object which is connected to the server we want to save to
//                   aDB - DBDefinition object for the database where items will be saved to
//             aRecordId - ID of the record to be updated, or 0 for a new record (Currently always 0)
//              aDBItems - Items to be saved under this record
function dbPreSaveHook(aDBClient, aDB, aRecordId, aDBItems, aShowErrors, aInteractive)
{
        if (!(aDB.hasField("Molecule", "Project") && aDB.hasField("Molecule", "NotebookRef")))
                return;

        // Construct a dialog for Project Number and Notebook Reference Number
        var dialog = new Dialog();
        dialog.title = "Data Fields";

        var gbMain = new GroupBox();
        gbMain.title = "Mandatory Fields";
        dialog.add(gbMain);

        var leProject = new LineEdit();
        leProject.label = "Project";
        gbMain.add(leProject);

        var leNotebookRef = new LineEdit();
        leNotebookRef.label = "Notebook Ref.";
        gbMain.add(leNotebookRef);

        // Pop up the dialog until either all fields are filled in or cancel is pressed
        var done = false;
        while (!done && aDBItems.length > 0)
        {
                var result = dialog.exec();
                if (result == 0)
                {
                        MessageBox.information("Save cancelled.");
                        aDBItems.length = 0;
                }
                else if (!leProject.text.length > 0)
                        MessageBox.information("You need to fill in the project number.");
                else if (!leNotebookRef.text.length > 0)
                        MessageBox.information("You need to fill in the Notebook reference.");
                else
                        done = true;
        }

        // Set item fields to the entered values
        for (var i=0; i<aDBItems.length; i++)
        {
                if (aDBItems[i].type == "Molecule")
                {
                        aDBItems[i].setField("Project", leProject.text);
                        aDBItems[i].setField("NotebookRef", leNotebookRef.text);
                }
        }
}

